let app = getApp()
let lastY = 0
let scale = 750 / wx.getSystemInfoSync().windowWidth
Component({
    properties: {
        cancel:Number,
        tab:Number,
        page_type:Number,
        pagetitle:String
      },
      data:{
        noMore: false,
        statusList: [ {
          title: "全部",
          id: -1
      }, {
          title: "待付款",
          id: 0
      }, {
          title: "待发货",
          id: 1
      }, {
          title: "待收货",
          id: 2
      }, {
          title: "待评价",
          id: 3
      }, {
          title: "退款售后",
          id: 5
      } ],
      init: !0,
      top:60,
      commentVisible: !1,
      commentShow: !1,
      commentAnimation: !0,
      refundVisible: !1,
      refundShow: !1,
      refundAnimation: !0,
      orderType: 0,
      activeCommentGood: {},
      activeOrder: {},
      refundReason: "",
      page_path: "pages/mine-order/mine-order",
      user_id:0,
      page:1,
      actions5: [
        {
            name: '取消'
        },
        {
            name: '确定',
            color: '#ed3f14',
            loading: false
        }
     ]
      },
     pageLifetimes: {
      show: function () {
            this.datainfo()
        }
      },
    lifetimes: {
        attached: function () {
          this.setData({
            user_id:wx.getStorageSync('user_id'),
            globalColor:app.globalData.globalColor
          })
          this.datainfo()
       } 
      },
  
    methods: {
      datainfo:function(){
        this.setData({
          page:1
        })
        if(this.data.page_type==4)//拼团
        {
          this.setData({
            orderType:1,
            top:0
          })
          this.groupdata();
        }
        else if(this.data.page_type==2)//砍价
        {
          this.setData({
            orderType:2,
            top:0
          })
          this.Bargain_getBargainUserList();
        }
        else if(this.data.page_type==3)//秒杀
        {
          this.setData({
            orderType:3,
            top:0
          })
          this.Seckill_getSeckillList();
        }
        else{
          this.getdata();
        }
      },
      cardinfo:function(e){
        var virtualCard=this.data.list[e.currentTarget.dataset.key]
        this.triggerEvent ('virtualcard',virtualCard.electronic_card[0])
      },
      bargain:function(e){
        const order_id = e.currentTarget.dataset.id;
        const activity_id = e.currentTarget.dataset.activity_id;
        wx.navigateTo({
          url: '/yb_shopv2/native/all/index?type=bargain_detail&orderid='+order_id+'&activity_id='+activity_id,
        })
      },
      orderinfo:function(e){
        const order_id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/yb_shopv2/native/all/index?type=order_detail&orderid='+order_id,
        })
      },
      express_info:function(e){
        const order_id = e.currentTarget.dataset.id;
        const index = e.currentTarget.dataset.key;
        wx.navigateTo({
          url: '/yb_shopv2/native/all/index?type=express&orderid='+order_id,
        })
      },
      changestatus:function(e){
        var tab=e.detail.cur
        this.setData({
          tab:tab,
          page:1
        })
        this.getdata();
      },
      cancelapply:function(e){
        this.setData({
          refundAnimation:!0,
          refundVisible:false,
          refundShow:true,
          refundVisible:false
        })
      },
      refund_reason_change:function(e){
          this.setData({
            refund_reason:e.detail.value
          })
      },
      surereceipt:function(e){
        const order_id = e.currentTarget.dataset.id;
        const index = e.currentTarget.dataset.key;
        var that=this
        wx.showModal({
          title: '提示',
          content: '确认已经收到买的物品？',
          showCancel: true,
          cancelText:"取消",
          confirmText:"确定",
          confirmColor: '#ff4040',
          success: function (res) {
            if (res.cancel) {
           } else{
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Order_updOrderStatus',
                user_id:that.data.user_id,
                order_id:order_id,
                status:3
              },
              success: function(res) {
                var list=that.data.list
                list[index]['order_status']=3
                that.setData({
                  list:list,
                  tab:3
                })
                that.getdata();
              }
            })
            wx.showToast({
              title: '确认成功！',
              icon: 'none',
              duration: 1500
            })
           }
           },
           fail: function (res) { },
           complete: function (res) { }
      })
      },
      cancelapplyresult:function(e){
        const order_id = e.currentTarget.dataset.id;
        const index = e.currentTarget.dataset.key;
        var that=this
        wx.showModal({
          title: '提示',
          content: '确认取消售后？',
          showCancel: true,
          cancelText:"取消",
          confirmText:"确定",
          confirmColor: '#ff4040',
          success: function (res) {
            if (res.cancel) {
           } else{
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Order_updOrderStatus',
                user_id:that.data.user_id,
                order_id:order_id,
                status:-2
              },
              success: function(res) {
                var list=that.data.list
                list[index]['order_status']=4
                that.setData({
                  list:list
                })
                that.datainfo();
              }
            })
            wx.showToast({
              title: '取消成功！',
              icon: 'none',
              duration: 1500
            })
           }
           },
           fail: function (res) { },
           complete: function (res) { }
      })
      },
      applysubmit:function(){
        if(typeof(this.data.refund_reason)!='undefined'&&this.data.refund_reason!='')
        {
          var that=this
          wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Order_updOrderStatus',
                order_id:that.data.refund_id,
                user_id:that.data.user_id,
                status:4,
                refund_reason:that.data.refund_reason
              },
              success: function(res) {
                var refund_index=that.data.refund_index
                var list=that.data.list
                list[refund_index]['order_status']=4
                that.cancelapply()
                that.setData({
                  list:list
                })
                wx.showToast({
                  title: '提交成功',
                  icon: 'success', 
                  duration: 1500 
              })
              }
            })
        }else{
          wx.showToast({
            title: '请输入售后信息',
            icon: 'none', 
            duration: 1500 
        })
        }
     
      },
      apply:function(e){
        const order_id = e.currentTarget.dataset.id;
        const index = e.currentTarget.dataset.key;
        var list=this.data.list
        this.setData({
            refund_id:order_id,
            refund_index:index,
            activeOrder:list[index].goods_item,
            refundAnimation:!1,
            refundVisible:true,
            refundShow:false,
            refundVisible:true
          })
      },
      Seckill_getSeckillList:function(){
        var that=this
        wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Order_userOrderList',
              user_id:that.data.user_id,
              page:that.data.page,
              status:-1,
              order_type:3
            },
            success: function(res) {
              if(res.data.data.length>0)
              {
               that.setData({
                 list:res.data.data,
                 page:that.data.page+1,
                 order_id:res.data.data[0].order_id
               })
              }
            }
          })
      },
      Bargain_getBargainUserList:function(){
        var that=this
        wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Bargain_getBargainUserList',
              user_id:that.data.user_id,
              page:that.data.page,
              status:-1
            },
            success: function(res) {
              if(res.data.data.length>0)
              {
               that.setData({
                 list:res.data.data,
                 page:that.data.page+1,
                 order_id:res.data.data[0].order_id
               })
              }
            }
          })
      },
      groupdata:function(){
        var that=this
        wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Order_userOrderList',
              user_id:that.data.user_id,
              page:that.data.page,
              status:-1,
              order_type:4
            },
            success: function(res) {
              if(res.data.data.length>0)
              {
               that.setData({
                 list:res.data.data,
                 page:that.data.page+1,
                 order_id:res.data.data[0].order_id
               })
              }
            }
          })
      },
      getdata:function(){
        if(this.data.tab<5){
          var orderstatus=this.data.tab-1
        }else{
          var orderstatus=this.data.tab
        }
        var that=this
         wx.request({
             url: app.globalData.siteroot,
             data: {
               do:'Order_userOrderList',
               user_id:that.data.user_id,
               page:that.data.page,
               status:orderstatus
             },
             success: function(res) {
               if(res.data.data.length>0)
               {
                that.setData({
                  list:res.data.data,
                  page:that.data.page+1,
                  order_id:res.data.data[0].order_id
                })
               }
               else
               {
                  that.setData({
                     list:res.data.data
                  })
               }
             }
           })
      },
      delorder:function(e){
        const order_id = e.currentTarget.dataset.id;
        const index = e.currentTarget.dataset.key;
        const user_id=this.data.user_id
        var list=this.data.list
        list[index]['order_status']=6;
        var that=this
        wx.showModal({
          title: '提示',
          content: '确认将该订单删除？删除后无法恢复',
          showCancel: true,
          cancelText:"取消",
          confirmText:"确定",
          confirmColor: '#ff4040',
          success: function (res) {
            if (res.cancel) {
           } else{
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Order_updOrderStatus',
                user_id:user_id,
                order_id:order_id,
                status:-1
              },
              success: function(res) {
                that.setData({
                  list:list
                })
                that.datainfo();
              }
            })
            wx.showToast({
              title: '删除成功！',
              icon: 'none',
              duration: 1500
            })
           }
           },
           fail: function (res) { },
           complete: function (res) { }
      })
      },
      payorder:function(e){
        const order_no = e.currentTarget.dataset.orderno;
        wx.setStorageSync('order_id',e.currentTarget.dataset.id);
        const user_id=this.data.user_id
        var that=this
        wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Pay_orderPay',
              order_no:order_no,
              order_type:0,
              user_id:user_id
            },
            success: function(res) {
                wx.requestPayment({
                    timeStamp: res.data.info.timeStamp,
                    nonceStr: res.data.info.nonceStr,
                    package: res.data.info.package,
                    signType: res.data.info.signType,
                    paySign: res.data.info.paySign,
                    success(res1) {
                      wx.reLaunch({
                        url: '/yb_shopv2/native/all/index?type=pay_result'
                      })
                    },
                    fail(res1) {
                        console.log('支付失败')
                    }
                  })
            }
          })
      },
      cancelorder:function(e){
        const order_id = e.currentTarget.dataset.id;
        const index = e.currentTarget.dataset.key;
        const user_id=this.data.user_id
        var list=this.data.list
        var that=this
        list[index]['order_status']=6;
        wx.showModal({
          title: '提示',
          content: '确认取消该订单？',
          showCancel: true,
          cancelText:"取消",
          confirmText:"确定",
          confirmColor: '#ff4040',
          success: function (res) {
            if (res.cancel) {
            } else{
              wx.request({
                url: app.globalData.siteroot,
                data: {
                  do:'Order_updOrderStatus',
                  user_id:user_id,
                  order_id:order_id,
                  status:6
                },
                success: function(res) {
                  wx.showToast({
                    title: '取消成功！',
                    icon: 'none',
                    duration: 1500
                  })
                  that.setData({
                    page:1
                  })
                  that.datainfo();
                }
              })
            }
           },
           fail: function (res) { },
           complete: function (res) { }
      })
      },
      groupclick:function(e){
        const order_id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/yb_shopv2/native/all/index?type=invitation_group&orderid='+order_id,
        })
      },
      scrollToLower: function(e) {
        var orderstatus=this.data.tab-1
        if (!this.data.noMore) {
          var that=this
        if(this.data.page_type==4)
        {
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Order_userOrderList',
              user_id:that.data.user_id,
              page:that.data.page,
              status:orderstatus,
              order_type:4
            },
            success: function(res) {
              if(res.data.data.length>0)
              {
                if(res.data.data[0].order_id!=that.data.order_id)
                {
                 that.data.list.concat(res.data.data)
                 that.setData({
                   page:that.data.page+1,
                   order_id:res.data.data[0].order_id,
                   list:that.data.list.concat(res.data.data)
                 })
                }
              }
            }
          })
        }
        else if(this.data.page_type==2)
        {
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Bargain_getBargainUserList',
              user_id:that.data.user_id,
              page:that.data.page,
              status:-1
            },
            success: function(res) {
              if(res.data.data.length>0)
              {
                if(res.data.data[0].order_id!=that.data.order_id)
                {
                 that.data.list.concat(res.data.data)
                 that.setData({
                   order_id:res.data.data[0].order_id,
                   page:that.data.page+1,
                   list:that.data.list.concat(res.data.data)
                 })
                }
              }
            }
          })
        }
        else if(this.data.page_type==3)
        {
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Order_userOrderList',
              user_id:that.data.user_id,
              status:-1,
              page:that.data.page+1,
              order_type:3
            },
            success: function(res) {
              if(res.data.data.length>0)
              {
                if(res.data.data[0].order_id!=that.data.order_id)
                {
                 that.data.list.concat(res.data.data)
                 that.setData({
                   order_id:res.data.data[0].order_id,
                   page:that.data.page+1,
                   list:that.data.list.concat(res.data.data)
                 })
                }
              }
            }
          })
        }
        else{
          var orderstatus=this.data.tab-1
           wx.request({
               url: app.globalData.siteroot,
               data: {
                 do:'Order_userOrderList',
                 user_id:that.data.user_id,
                 page:that.data.page,
                 status:orderstatus
               },
               success: function(res) {
                 if(res.data.data.length>0)
                 {
                  if(res.data.data[0].order_id!=that.data.order_id)
                  {
                   that.data.list.concat(res.data.data)
                   that.setData({
                     order_id:res.data.data[0].order_id,
                     page:that.data.page+1,
                     list:that.data.list.concat(res.data.data)
                   })
                  }
                 }
                 else
                 {
                  wx.showToast({
                    title: '暂无更多数据',
                    icon: 'none',
                    duration: 1500
                  })
                 }
               }
             })
        } 
        }
      }
    }
});
